package cn.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockThreadTest extends Thread {

    private Lock lock = new ReentrantLock();

    /**
     * Lock 获取锁 和释放锁 需要开放人员自己定义
     *
     * @param args
     */
    public static void main(String[] args) {
        LockThreadTest lockThreadTest = new LockThreadTest();
        lockThreadTest.testLock();
        lockThreadTest.testLock();
    }


    public void testLock() {
        Thread thread = new Thread(() -> {
            count();
            System.out.println(Thread.currentThread().getName() + "获取锁成功");
            lock.unlock();
            System.out.println(Thread.currentThread().getName() + "解锁成功");
        });
        thread.start();
    }

    public void count() {
        try {
            lock.lock();
            System.out.println("Lock Success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
